#include <Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include "cTools.h"
#include "data.h"
#include "d3dbase.h"
#pragma comment(lib, "d3dx9.lib")
VOID DrawTxt(INT X, INT Y, D3DCOLOR Color, ID3DXFont *font, CONST PCHAR Text, DWORD flag = 0);

pReset oReset;
pEndScene oEndScene;

ID3DXFont* pFont, *pFontTitle, *pPresentFont;
cTools _cTools, *pTools = &_cTools;

cTimer Timer;
cTime Time;

#define FREE_MEM(i) ( (unsigned long)GetModuleHandleA("d3d9.dll") + 0x1000 - ( i? i*0x20:0x20 ) ) 
#define D3DCOLOR_RED D3DCOLOR_ARGB(100,255,69,0)

HRESULT WINAPI hkReset (LPDIRECT3DDEVICE9 pDevice, D3DPRESENT_PARAMETERS* pPresentationParameters) 
{
	__asm NOP;
	if(pFont){
		pFont->OnLostDevice();
		pFontTitle->OnLostDevice();
	}
	
	HRESULT hRet = oReset(pDevice, pPresentationParameters);

	if(hRet == D3D_OK){
		if(pFont){
			pFont->OnResetDevice();
			pFontTitle->OnResetDevice();
		}
	}
	return hRet;
}

HRESULT WINAPI hkEndScene(LPDIRECT3DDEVICE9 pDevice)
 {
	__asm NOP;
	HRESULT hRet = oEndScene(pDevice);
	
	static bool bInit = false, bMenu = false, blol = false;
	static char RedBufor[20] = {0}, BlueBufor[20] = {0},DragonBufor[20] = {0},NashorBufor[20] = {0},WraithsBufor[20] = {0},WolvesBufor[20] = {0},GolemsBufor[20] = {0};
	D3DVIEWPORT9 viewP;
	static DWORD ScreenCenterX, ScreenCenterY;
	static int fontsize = 10;
	if(!bInit){
		const long nScreenWidth = ::GetSystemMetrics(SM_CXSCREEN);
		const long nScreenHeight = ::GetSystemMetrics(SM_CYSCREEN);

		if(nScreenWidth == 1680 && nScreenHeight == 1050)
			fontsize = 18;
		else if(nScreenWidth == 1600 && nScreenHeight == 900)
			fontsize = 17;
		else if(nScreenWidth == 1440 && nScreenHeight == 900)
			fontsize = 17;
		else if(nScreenWidth == 1280 && nScreenHeight == 1024)
			fontsize = 16;
		else if(nScreenWidth == 1280 && nScreenHeight == 960)
			fontsize = 16;
		else if(nScreenWidth == 1152 && nScreenHeight == 864)
			fontsize = 15;
		else if(nScreenWidth == 1024 && nScreenHeight == 768)
			fontsize = 15;
	
		pDevice->GetViewport( &viewP );
		ScreenCenterX = viewP.Width / 2; 
		ScreenCenterY = viewP.Height / 2;
		D3DXCreateFontA( pDevice,17, 0,620 , 0, FALSE,ANSI_CHARSET, OUT_TT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Verdana", &pFont);
		D3DXCreateFontA( pDevice, 26, 0,700 , 0, FALSE,ANSI_CHARSET, OUT_TT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Orator Std", &pFontTitle);
		bInit = true;
	}
	
	if(GetAsyncKeyState(VK_HOME) &1)
		bMenu = !bMenu;
		
	if(Timer.BlueStart){
		sprintf(BlueBufor, "Blue: %d", Time.BlueTime);
		pTools->DrawTxt(ScreenCenterX-310,15, D3DCOLOR_RED, pFont,BlueBufor ); 
		if(Timer.BlueSpawned){
			Timer.BlueStart = Timer.BlueSpawned = 0;
		}
	}

	if(Timer.RedStart){
		sprintf(RedBufor, "Red: %d", Time.RedTime);
		pTools->DrawTxt(ScreenCenterX-210,15, D3DCOLOR_RED, pFont,RedBufor ); 
		if(Timer.RedSpawned){
			Timer.RedStart = Timer.RedSpawned = 0;
		}
	}
	if(Timer.DragonStart){
		sprintf(DragonBufor, "Dragon: %d", Time.DragonTime);
		pTools->DrawTxt(ScreenCenterX-110,15, D3DCOLOR_RED, pFont,DragonBufor ); 
		if(Timer.DragonSpawned){
			Timer.DragonStart = Timer.DragonSpawned = 0;
		}
	}
	if(Timer.NashorStart){
		sprintf(NashorBufor, "Nashor: %d", Time.NashorTime);
		pTools->DrawTxt(ScreenCenterX,15, D3DCOLOR_RED, pFont,NashorBufor ); 
		if(Timer.NashorSpawned){
			Timer.NashorStart = Timer.NashorSpawned = 0;
		}
	}
	if(Timer.WolvesStart){
		sprintf(WolvesBufor, "Wolves: %d", Time.WolvesTime);
		pTools->DrawTxt(ScreenCenterX+110,15, D3DCOLOR_RED, pFont,WolvesBufor ); 
		if(Timer.WolvesSpawned){
			Timer.WolvesStart = Timer.WolvesSpawned = 0;
		}
	}
	if(Timer.WraithsStart){
		sprintf(WraithsBufor, "Wraiths: %d", Time.WraithsTime);
		pTools->DrawTxt(ScreenCenterX+210,15, D3DCOLOR_RED, pFont,WraithsBufor ); 
		if(Timer.WraithsSpawned){
			Timer.WraithsStart = Timer.WraithsSpawned = 0;
		}
	}
	if(Timer.GolemsStart){
		sprintf(GolemsBufor, "Golems: %d", Time.GolemsTime);
		pTools->DrawTxt(ScreenCenterX+310,15, D3DCOLOR_RED, pFont,GolemsBufor ); 
		if(Timer.GolemsSpawned){
			Timer.GolemsStart = Timer.GolemsSpawned = 0;
		}
	}

	return hRet;
 }

DWORD WINAPI HookThread( LPVOID lpParam )
{
	unsigned long** ulDevicePtr = pTools->FindDevice();
	unsigned long ** VTable ;
	*(unsigned long **)&VTable = *(unsigned long **)ulDevicePtr;
	
	HMODULE mod = 0;
	while(!mod){
		 mod = GetModuleHandleA("d3d9.dll");
		 Sleep(50);
	}mod = 0;
	
	while((1-1+111)){ 
		Sleep(300);
		if(GetAsyncKeyState(VK_F12))
			return 0;
		if(*(BYTE*)VTable[42] != 0xEB){
			pTools->MemXorJmp(FREE_MEM(0), (unsigned long)hkEndScene);
			oEndScene = (pEndScene)pTools->MemHotPatch((unsigned long)VTable[42], FREE_MEM(0) );
		}
		else if(*(BYTE*)VTable[16] != 0xEB){
			pTools->MemXorJmp(FREE_MEM(2), (unsigned long)hkReset); 
			oReset = (pReset)pTools->MemHotPatch((unsigned long)VTable[16], FREE_MEM(2) );
		}
	}

	return true;
}